home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / QuitSomethingElse / QuitSomethingElse App / QuitApp.c next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  5.5 KB  |  239 lines

  1. /* #include files */
  2. #include <Types.h>
  3. #include <Memory.h>
  4. #include <Quickdraw.h>
  5. #include <Fonts.h>
  6. #include <Events.h>
  7. #include <Menus.h>
  8. #include <Windows.h>
  9. #include <TextEdit.h>
  10. #include <Dialogs.h>
  11. #include <OSUtils.h>
  12. #include <ToolUtils.h>
  13. #include <TextUtils.h>
  14. #include <SegLoad.h>
  15. #include <Sound.h>
  16.  
  17. /* #defines */
  18. #define kQuitDialogResId 128
  19. #define kPopupMenuResId 128
  20.  
  21. #define kOkButton 1
  22. #define kCancelButton 2
  23. #define kPopupMenu 3
  24. #define kAlertIcon 4
  25. #define kPromptString1 5
  26. #define kPromptString2 6
  27.  
  28. //#define kTestString1 "\pThere is not enough memory to open "
  29. //#define kTestString2 "\pSelect one or more applications to quit from the list below. Then click \"Quit Applications\" to reopen \""
  30.  
  31. #define kThisAppSig 'ASd0'
  32.  
  33. #define kErrorALRTid        128
  34.  
  35. #define kNoIdleProc            nil
  36. #define kNoFilterProc        nil
  37.  
  38. /* Constants */
  39.  
  40. /* Types */
  41.  
  42. /* Globals */
  43.     
  44. /* Prototypes */
  45. void Initialize( void );
  46. void HandleDialog( void );
  47. void SendAppleEvent( long targetSignature );
  48. //pascal Boolean OurFilter( DialogPtr dlg, EventRecord *event, short *itemHit );
  49.  
  50. int main( void ) {
  51.     Initialize();
  52.     
  53.     HandleDialog();
  54.  
  55.     return 0;    
  56. }
  57.  
  58. void Initialize( void ) {
  59.     InitGraf( &qd.thePort );
  60.     InitFonts();
  61.     InitWindows();
  62.     InitMenus();
  63.     TEInit();
  64.     InitDialogs( nil );
  65.     InitCursor();
  66.     
  67.     FlushEvents( everyEvent, 0 );
  68. }
  69.  
  70. void HandleDialog( void ) {
  71.     DialogPtr theDialog;
  72.     DialogItemIndex itemHit = -1;
  73.     Boolean    result = false;
  74.     GrafPtr oldPort;
  75.     DialogItemType itemType;
  76.     Handle item;
  77.     Rect box;
  78.     MenuHandle theMenu = nil;
  79.     ProcessSerialNumber thePSN;
  80.     ProcessInfoRec info;
  81.     FSSpec processFSSpec;
  82.     long menuSelection = -1;
  83.     Str255 processName, tempName, matchName;
  84.     short itemCount = 1, i = 0, j = 0;
  85.     CharParameter markChar;
  86.     SInt16 theItem;
  87.     Style getStyle;
  88.     
  89.     theDialog = ( DialogPtr )GetNewDialog( kQuitDialogResId, 0L, ( WindowPtr )-1L );
  90.  
  91.     if ( theDialog == nil )
  92.         return;
  93.         
  94.     theMenu = GetMenu( kPopupMenuResId );
  95.  
  96.     if ( theMenu == nil )
  97.         goto cleanup;
  98.         
  99.     GetPort( &oldPort );
  100.     SetPort( theDialog );
  101.  
  102.     SetDialogDefaultItem( theDialog, ok ) ;
  103.  
  104. //    GetDialogItem( theDialog, kPromptString1, &itemType, &item, &box );
  105. //    SetDialogItemText( item, kTestString1 );
  106.  
  107.     HLock( ( Handle )theMenu );
  108.     
  109.     // check the current processes to see if the application is already
  110.     // running, and get its process serial number.
  111.     thePSN.lowLongOfPSN = kNoProcess;
  112.     thePSN.highLongOfPSN = 0;
  113.     
  114.     info.processInfoLength = sizeof( FSSpec );
  115.     info.processName = ( unsigned char * )processName;
  116.     info.processAppSpec = &processFSSpec;
  117.  
  118.     while ( noErr == GetNextProcess( &thePSN ) ) {
  119.         if ( noErr == GetProcessInformation( &thePSN, &info ) ) {
  120.             if ( info.processType == 'APPL' && info.processSignature != kThisAppSig ) {
  121.                 AppendMenu( theMenu, info.processName );
  122.             }
  123.         }
  124.     }
  125.  
  126.     SetControlValue( ( ControlHandle )theMenu, 1 );
  127.     Draw1Control( ( ControlHandle )theMenu );
  128.  
  129.     do {
  130.         ModalDialog( 0L, &itemHit );
  131.  
  132.         if ( itemHit == kPopupMenu ) {
  133.             // get the control handle for the popup            
  134.             GetDialogItem( theDialog, kPopupMenu, &itemType, &item, &box ) ;
  135.             theItem = GetControlValue( ( ControlHandle )item );
  136.             GetItemMark( theMenu, theItem, &markChar );
  137.             
  138.             GetMenuItemText( theMenu, theItem, tempName );
  139.  
  140.             j = tempName[ 0 ];
  141.  
  142.             GetItemStyle( theMenu, theItem, ( unsigned char * )&getStyle );
  143.             
  144.             if ( getStyle == bold ) {
  145.                 SetItemStyle( theMenu, theItem, normal );
  146.             }
  147.             else {
  148.                 SetItemStyle( theMenu, theItem, bold );
  149.             }
  150.  
  151.             // this ensures that the update handler in the filter proc is called
  152.             // as that is where we enable or disable the OK button
  153.             InvalRect( &box ) ;
  154.             
  155.             // reset itemHit to something else or we'll continually redraw
  156.             itemHit = 0;
  157.         }
  158.     } while ( itemHit != ok && itemHit != cancel );
  159.  
  160.     if ( itemHit != ok )
  161.         goto cleanup;
  162.  
  163.     itemCount = CountMItems( theMenu );
  164.     
  165.     for ( i = 1; i <= itemCount; i++ ) {
  166.         GetDialogItem( theDialog, kPopupMenu, &itemType, &item, &box );
  167.  
  168.         GetMenuItemText ( theMenu, i, matchName );
  169.  
  170.         GetItemStyle( theMenu, i, ( unsigned char * )&getStyle );
  171.         
  172.         if ( getStyle != bold )
  173.             continue;
  174.  
  175.         thePSN.lowLongOfPSN = kNoProcess;
  176.         thePSN.highLongOfPSN = 0;
  177.         
  178.         info.processInfoLength = sizeof( FSSpec );
  179.         info.processName = ( unsigned char * )processName;
  180.         info.processAppSpec = &processFSSpec;
  181.  
  182.         while ( noErr == GetNextProcess( &thePSN ) ) {
  183.             if ( noErr == GetProcessInformation( &thePSN, &info ) ) {
  184.                 if ( info.processType == 'APPL' && info.processSignature != kThisAppSig ) {
  185.                     j = 0;
  186.                     
  187.                     while ( j <= info.processName[ 0 ] ) {
  188.                         tempName[ j ] = info.processName[ j ];
  189.                         j++;
  190.                     }
  191.                     
  192.                     tempName[ 0 ] = info.processName[ 0 ];
  193.  
  194.                     if ( EqualString( tempName, matchName, false, false ) ) {
  195.                         SendAppleEvent( info.processSignature );
  196.                         break;
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203. cleanup:
  204.     HUnlock( ( Handle )theMenu );
  205.  
  206.     DisposeDialog( theDialog );
  207.  
  208.     SetPort( oldPort );    
  209. }
  210.  
  211. void SendAppleEvent( long targetSignature ) {
  212.     AEAddressDesc    targetAddrDesc = {typeNull, nil};
  213.     AppleEvent        event = {typeNull, nil};
  214.     AppleEvent        reply = {typeNull, nil};
  215.     OSErr            err;
  216.     
  217.     err = AECreateDesc( typeApplSignature, (Ptr)(&targetSignature),
  218.                 sizeof( targetSignature ), &targetAddrDesc );
  219.     
  220.     if ( err != noErr )
  221.         goto cleanup;
  222.         
  223.     err = AECreateAppleEvent( kCoreEventClass, kAEQuitApplication,
  224.                             &targetAddrDesc, kAutoGenerateReturnID,
  225.                             kAnyTransactionID, &event );
  226.     
  227.     if ( err != noErr )
  228.         goto cleanup;
  229.  
  230.     err = AESend( &event, &reply, kAENoReply,
  231.                 kAENormalPriority, kAEDefaultTimeout, kNoIdleProc, kNoFilterProc );
  232.  
  233. cleanup:
  234.     AEDisposeDesc (&targetAddrDesc);
  235.     AEDisposeDesc (&event);
  236.     AEDisposeDesc (&reply);    
  237. }
  238.  
  239.